<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>this is javascript demo</title>
<script type="text/javascript">
	var hanoi=function(disk, src,tmp,dst){
		if (disk>0) {
			hanoi(disk-1,src,dst,tmp);
			document.writeln("move disk "+disk+" from "+src+ " to "+dst+'\n');
			hanoi(disk-1,tmp,src,dst);
		};
	};

	var person={
		name:'charles',
		age:23
	};

	//返回一个以传进来的对象作为原型的对象
	Object.beget = function(o) {
		var F = function(){};
		F.prototype = o;
		return new F();
	};

	var anthor_person = Object.beget(person);
	document.writeln("name: " + anthor_person.name+"  age:  "+anthor_person.age);//这里会拿到person中的name、age

	anthor_person['name']='tom';
	document.writeln("name: " + anthor_person.name+"  age:  "+anthor_person.age);//改变这个对象值不会改变原型中的值
	document.writeln("name: " + person.name+"  age:  "+person.age);//还是原来的值

	Function.prototype.addMethod = function(n,func) {
		this.prototype[n] = func;//这里不能这么写：this.prototype.n = func; 为什么？
	};

	String.addMethod('deentityify',function(){
		var entity = {
			quot:'"',
			lt : '<',
			gt: '>'
		};

		return function(){
			return this.replace(/&([^&;]+);/g, 
				function(a,b){
					var r = entity[b];
					return typeof r === 'string'? r:a;
				}
			);
		};
	}());
	document.writeln(typeof "&lt;&quot;&gt;");
	document.writeln("&lt;&quot;&gt;".deentityify());//没有试验成功


	var serial_mark = function() {
		var prefix = '';
		var seq = 0;
		return {
			set_prefix : function (p){
				prefix=String(p);
			},
			set_seq : function(s) {
				seq = s;
			},
			gensym: function(){
				var result = prefix+seq;
				seq = seq + 1;
				return result;
			}
		};
	};
	//debugger;
	var seqer = serial_mark();
	seqer.set_prefix("A");
	seqer.set_seq(0001);
	document.writeln(seqer.gensym());
	document.writeln(seqer.gensym());

	var add = function (a,b){
		return a+b;
	};

	Function.addMethod('curry',function() {
		var that = this;
		var slice = Array.prototype.slice;
		args = slice.apply(arguments);
		return function() {
			return that.apply(null,args.concat(slice.apply(arguments)));
		};
	});

	var add1 = add.curry(1);
	document.writeln(add1(6));

	var memoizer = function(memo,fundemetal) {
		var shell = function(n) {
			var result = memo[n];
			if (typeof result !== 'number') {
				result = fundemetal(shell,n);
				memo[n] = result;
			};
			return result;
		};
		return shell;
	};

	var fibonacci = memoizer([0,1],function(shell,n) {
		return shell(n-1)+shell(n-2);
	});
	document.writeln(fibonacci(10));
</script>
</head>

<body>
	<h1>这是一个javascript学习练习</h1>
	<script type="text/javascript">
		hanoi(2,'src','tmp','dst');
	</script>
	
	<script type="text/javascript">
		//演示作用域
		var scope = function(){
			var a = 1;	//var 定义的变量作用域范围是函数内的，去掉var，a在函数外也是可以访问的
			document.writeln(a);
			//var a = 1 ;   //如果将a定义在这里，上面将不能打印a的值，也就是“在函数内任意地方定义的值在函数的任意地方都是可见的”这句话不对
		}
		scope();
		//document.writeln(a);
	</script>
</body>

</html>